package com.share.module.business.stockrevenuequarter.service;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.module.business.stock.dao.StockDAO;
import com.share.module.business.stock.entity.Stock;
import com.share.module.business.stockrevenuequarter.dao.StockRevenueQuarterDAO;
import com.share.module.business.stockrevenuequarter.entity.StockRevenueQuarter;
import com.share.module.business.stockrevenuequarter.vo.StockRevenueQuarterPageQueryVO;
import com.share.module.business.stockrevenuequarter.vo.StockRevenueQuarterPageVO;
import com.stock.common.lang.StringUtils;
import com.stock.mybatisplus.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 营业总收入-单季度 服务类
 * </p>
 *
 * @author lijy
 * @since 2025-11-04
 */
@Service
public class StockRevenueQuarterService extends ServiceImpl<StockRevenueQuarterDAO, StockRevenueQuarter> {

    @Resource
    private StockDAO stockDAO;

    /**
     * 分页查询营业总收入-单季度列表
     */
    public PageUtils<StockRevenueQuarterPageVO> page(StockRevenueQuarterPageQueryVO vo) {
        LambdaQueryWrapper<StockRevenueQuarter> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(vo.getStockCode()), StockRevenueQuarter::getStockCode, vo.getStockCode());
        wrapper.eq(vo.getFiscalYear() != null, StockRevenueQuarter::getFiscalYear, vo.getFiscalYear());
        wrapper.orderByDesc(StockRevenueQuarter::getCreateDate);
        IPage<StockRevenueQuarter> page = super.page(new Page<>(vo.getCurrPage(), vo.getSize()), wrapper);

        // 提取股票代码
        List<String> stockCodes = page.getRecords().stream().map(StockRevenueQuarter::getStockCode).distinct().collect(Collectors.toList());
        if (stockCodes.isEmpty()) {
            return new PageUtils<>(new ArrayList<>(), page.getTotal(), page.getSize(), page.getCurrent());
        }
        List<Stock> stocks = stockDAO.findCodeNameByCodes(stockCodes);
        Map<String, String> codeNameMap = stocks.stream().collect(Collectors.toMap(Stock::getCode, Stock::getName));

        List<StockRevenueQuarterPageVO> targets = page.getRecords().stream().map(item -> {
            StockRevenueQuarterPageVO pageVO = new StockRevenueQuarterPageVO();
            BeanUtils.copyProperties(item, pageVO);
            pageVO.setStockName(codeNameMap.get(item.getStockCode()));
            pageVO.setTotalRevenue(NumberUtil.add(item.getQ1Revenue(), item.getQ2Revenue(), item.getQ3Revenue(), item.getQ4Revenue()));
            return pageVO;
        }).collect(Collectors.toList());

        return new PageUtils<>(targets, page.getTotal(), page.getSize(), page.getCurrent());
    }

    public List<StockRevenueQuarter> findByStockCode(String stockCode) {
        LambdaQueryWrapper<StockRevenueQuarter> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StockRevenueQuarter::getStockCode, stockCode);
        wrapper.orderByAsc(StockRevenueQuarter::getFiscalYear);
        return list(wrapper);
    }
}
